let serverUrl = 'http://119.96.123.100';
// let serverUrl = 'http://localhost';
let serverPort = '9527';
let serverName = '/module';

function changeStrTime(time) {
    if (null === time || '' === time || typeof time === "undefined" || time.length < 12 || time === 0) {
        return "";
    } else {
        if (time.indexOf('-') > 0) {
            return time.substring(0, 16);
        } else {
            if (time.length <= 13) {
                return formatTime(Number(time));
            } else {
                return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10) + ":" + time.substring(10, 12);
            }
        }
    }
}

// 创建遮罩层并添加到页面
function createMask() {
    let mask = document.createElement('div');
    mask.className = 'mask';
    mask.innerHTML = '<p class="mask-info">正在加载，请稍后...</p>';
    document.body.appendChild(mask);
    return mask;
}

// 显示加载动画
function showLoading() {
    let mask = document.querySelector('.mask');

    if (!mask) {
        mask = createMask();
    }

    mask.style.display = 'block';

    setTimeout(function () {
        mask.style.opacity = '1'; // 渐显
    }, 10);
}

// 隐藏加载动画
function hideLoading() {
    let mask = document.querySelector('.mask');

    if (mask) {
        mask.style.opacity = '0'; // 渐隐

        setTimeout(function () {
            mask.style.display = 'none';
        }, 300); // 与CSS动画时间一致
    }
}

function formatStr(str, count) {
    if (str.length > count) {
        return str.substring(0, count) + "...";
    } else {
        return str;
    }
}

// 时间戳转换函数
function formatTime(timestamp) {
    // 验证输入
    if (null == timestamp) return "暂无时间";
    if (isNaN(timestamp)) return "暂无时间";

    // 检查时间戳是否在有效范围内
    if (timestamp < -8640000000000000 || timestamp > 8640000000000000) {
        return "暂无时间";
    }

    // 创建Date对象
    const date = new Date(timestamp);

    // 验证Date对象是否有效
    if (isNaN(date.getTime())) {
        return "暂无时间";
    }

    // 获取各个时间组件
    let year = date.getFullYear(), month = date.getMonth() + 1, //月份是从0开始的
        day = date.getDate(), hour = date.getHours(), min = date.getMinutes(), sec = date.getSeconds();
    return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min);
}

// 创建表格
function createTable(tableId, title, data) {
    const table = document.getElementById(tableId);
    table.innerHTML = '';

    // 创建表头
    const thead = document.createElement('thead');
    const headerRow = document.createElement('tr');
    title.forEach(text => {
        const th = document.createElement('th');
        th.textContent = text;
        th.style.whiteSpace = 'normal';
        headerRow.appendChild(th);
    });
    thead.appendChild(headerRow);
    table.appendChild(thead);

    // 创建表格体
    const tbody = document.createElement('tbody');
    data.forEach(rowData => {
        const tr = document.createElement('tr');
        rowData.forEach((cell, index) => {
            if (index === (rowData.length - 1)) {
                tr.setAttribute("id", cell);
            } else {
                const td = document.createElement('td');
                let tdWidth = '100px';

                td.setAttribute("data-index", index);

                if (typeof cell !== "undefined") {
                    td.textContent = cell;
                } else {
                    td.textContent = '-';
                }

                if (index === 0) {
                    td.style.fontWeight = 'bold';
                    td.style.width = '100px';
                } else {
                    tdWidth = ((td.textContent.length * 10) + 'px');
                    td.style.width = tdWidth;
                }

                tr.appendChild(td);
            }
        });
        tbody.appendChild(tr);
    });
    table.appendChild(tbody);
}

function showMsg(content) {
    const statusEl = document.createElement('div');
    statusEl.className = 'status-message';
    statusEl.textContent = content;
    statusEl.style.cssText = `
	    position: fixed;
	    bottom: 70px;
	    left: 50%;
	    transform: translateX(-50%);
	    background: rgba(0,0,0,0.7);
	    color: white;
	    padding: 10px 20px;
	    border-radius: 20px;
	    font-size: 14px;
	    z-index: 1000;
	    animation: fadeInOut 2s ease-in-out;
	`;

    document.body.appendChild(statusEl);

    // 2秒后移除提示
    setTimeout(() => {
        statusEl.remove();
    }, 2000);
}

function goBack() {
    window.history.back();
}

function toPage(num) {
    switch (num) {
        case 1:
            // 用户管理
            sessionStorage.setItem("selectCompanyType", "user");
            window.location.href = 'wx/center/selectCompany';
            break;
        case 2:
            // 设备管理
            sessionStorage.setItem("selectCompanyType", "device");
            window.location.href = 'wx/center/selectCompany';
            break;
        case 3:
            // 公司列表
            window.location.href = 'wx/center/company';
            break;
        case 4:
            // 系统设置
            window.location.href = 'wx/center/setting';
            break;
        case 5:
            // 个人信息
            window.location.href = 'wx/center/personInfo';
            break;
        case 6:
            // 联系我们
            window.location.href = 'wx/center/contactUs';
            break;
        case 7:
            // 邀请加入
            window.location.href = 'wx/center/invitation';
            break;
    }
}